/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.authorize.api;

import lombok.extern.slf4j.Slf4j;
import org.cshoo.tattoo.authorize.api.dto.AddAuthorityReq;
import org.cshoo.tattoo.authorize.api.dto.AuthorityResp;
import org.cshoo.tattoo.authorize.app.AuthorityAppService;
import org.cshoo.tattoo.embed.infra.entity.Response;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨翼
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/authority")
@Slf4j
public class AuthorityController {

    @Resource
    private AuthorityAppService authorityAppService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public void addAuthority(@RequestBody AddAuthorityReq authorityReq) {
        authorityAppService.addAuthority(authorityReq);
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<AuthorityResp> findAllAuthorities() {
        log.info("findAllAuthorities...");
        return authorityAppService.findAllAuthority();
    }

    @RequestMapping(value = "/findByRoleId/{roleId}", method = RequestMethod.GET)
    public List<AuthorityResp> findByRoleId(@PathVariable("roleId") Long roleId) {
        log.info("roleId:{}", roleId);
        return authorityAppService.findByRoleId(roleId);
    }

    @RequestMapping(value = "/findByMicroservice/{microserviceId}", method = RequestMethod.GET)
    public List<AuthorityResp> findByMicroservice(@PathVariable("microserviceId") Long microserviceId) {
        log.info("findAuthoritiesByMicroservice...,microserviceId:{}", microserviceId);
        return authorityAppService.findByMicroserviceId(microserviceId);
    }

    @RequestMapping(value = "/refreshAuthorities/{microserviceId}", method = RequestMethod.PUT)
    public Response<?> refreshAuthorities(@PathVariable("microserviceId") Long microserviceId) {
        authorityAppService.refreshAuthorities(microserviceId);
        return Response.builder().build();
    }

    @RequestMapping(value = "/changeAccessScope", method = RequestMethod.PUT)
    public Response<?> changeAccessScope(@RequestParam("id") Long id, @RequestParam("accessScope") Integer accessScope) {
        log.info("path:{},accessScope:{}", id, accessScope);
        authorityAppService.fixAccessScope(id, accessScope);
        return Response.builder().build();
    }

}
